package com.bjpowernode;

import com.bjpowernode.entity.Student;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author shentianyu
 * @date 2021-07-03 16:13
 */
public class MyApp {
    public static void main(String[] args) throws IOException {
        //mybatis主配置文件的名称 从类路径根开始
        String config = "mybatis.xml";
        //读取这个文件
        InputStream in = Resources.getResourceAsStream(config);
        //创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //创建SqlSessionFactory对象
        SqlSessionFactory factory = builder.build(in);
        //获取SqlSession对象
        SqlSession session = factory.openSession();
        //指定要执行的sql语句标识
        //sql映射文件中的namespace+.+标签中的id值
        String sqlId = "com.bjpowernode.dao.StudentDao" + "." + "selectStudents";
        //执行sql语句
        List<Student> list = session.selectList(sqlId);

        list.forEach(System.out::println);

        //关闭sqlsession对象
        session.close();
    }
}
